<?php
class ControllerModuleLatest extends Controller {
    public function index($setting) {
        $this->language->load('module/latest');

        $this->data['heading_title'] = $this->language->get('heading_title');

        $this->data['button_cart'] = $this->language->get('button_cart');

        $this->app->model('product/model', true, true);

        $this->app->model('tool/image', true, true);

        $this->data['products'] = array();

        $data = array(
            'sort'  => 'p.date_added',
            'order' => 'DESC',
            'start' => 0,
            'limit' => $setting['limit']
        );

        if ($this->auth->is_logged()) {
            $user_group_id = $this->customer->get_group_id();
        } else {
            $user_group_id = $this->setting->get('config_user_group_id');
        }	

        $product_data = $this->cache->get('product.latest.' . (int)$this->setting->get('setting_language_id') . '.' . (int)$this->setting->get('setting_store_id'). '.' . $user_group_id . '.' . (int)$setting['limit']);

        if(!$product_data){
            $product_data = $this->model_product_model->getLatestProducts($setting['limit']);
            $this->cache->set('product.latest.' . (int)$this->setting->get('setting_language_id') . '.' . (int)$this->setting->get('setting_store_id'). '.' . $user_group_id . '.' . (int)$setting['limit'], $product_data);
        }

        foreach ($product_data as $result) {
            if ($result['image']) {
                $image = $this->model_tool_image->resize($result['image'], $setting['image_width'], $setting['image_height']);
            } else {
                $image = false;
            }

            if (($this->setting->get('config_customer_price') && $this->auth->is_logged()) || !$this->setting->get('config_customer_price')) {
                $price = $this->currency->format($this->tax->calculate($result['price'], $result['tax_class_id'], $this->setting->get('config_tax')));
            } else {
                $price = false;
            }

            if ((float)$result['special']) {
                $special = $this->currency->format($this->tax->calculate($result['special'], $result['tax_class_id'], $this->setting->get('config_tax')));
            } else {
                $special = false;
            }

            if ($this->setting->get('config_review_status')) {
                $rating = $result['rating'];
            } else {
                $rating = false;
            }

            $this->data['products'][] = array(
                'product_id' => $result['product_id'],
                'thumb'   	 => $image,
                'name'    	 => $result['name'],
                'price'   	 => $price,
                'special' 	 => $special,
                'rating'     => $rating,
                'reviews'    => sprintf($this->language->get('text_reviews'), (int)$result['reviews']),
                'href'    	 => $this->url->link('product/product', 'product_id=' . $result['product_id']),
            );
        }

        $this->template = 'module/latest.tpl';

        return $this->render();
    }
}
?>
